package com.ithiema.edu.info.manager.controller;

import com.ithiema.edu.info.manager.domain.Teacher;
import com.ithiema.edu.info.manager.service.TeacherService;
import com.ithiema.edu.info.manager.utils.Utils;

import java.util.Scanner;

public abstract class BaseTeacherController {
    Scanner sc = new Scanner(System.in);
    TeacherService teacherService = new TeacherService();
    Utils utils = new Utils();

    public void start() {
        loop:
        while (true) {
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加教师  2.删除教师  3.修改教师  4.查看教师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addTeacehr();
                    break;
                case 2:
                    deleteTeacehr();
                    break;
                case 3:
                    reviseTeacehr();
                    break;
                case 4:
                    showTeacehr();
                    break;
                case 5:
                    System.out.println("退出");
                    break loop;
                default:
                    System.out.println("您输入有误,请重新输入");
            }
        }
    }

    //查看教师信息
    private void showTeacehr() {
        teacherService.showTeacher();
    }

    //修改教师信息
    private void reviseTeacehr() {
        String id = input_id();
        if("0".equals(id)){
            return;
        }
        Teacher teacher = input_teacher(id);
        teacherService.reviseTeacehr(id, teacher);
    }

    //删除教师信息
    private void deleteTeacehr() {
        String id = input_id();
        if("0".equals(id)){
            return;
        }
        if (teacherService.deleteTeacehr(id)) {
            System.out.println("删除教师成功");
        }
    }

    //添加教师信息
    private void addTeacehr() {
        String id;
        System.out.println("请输入教师编号(输入0取消)");
        while (true) {
            id = sc.next();
            if ("0".equals(id)) {
                System.out.println("已取消");
                return;
            }
            if (!teacherService.seekIndex(id)) {
                break;
            }
            System.out.println("编号已被占用(输入0取消)");
        }
        //录入教师信息
        Teacher teacher = input_teacher(id);
        //考虑数组溢出问题，定义flag作为判断
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加教师成功");
        } else {
            System.out.println("添加失败，人数已达上限");
        }
    }

    //编号输入方法
    private String input_id() {
        String id;
        System.out.println("请输入教师编号(输入0取消)");
        while (true) {
            id = sc.next();
            if("0".equals(id)){
                System.out.println("已取消");
                return "0";
            }
            if(teacherService.seekIndex(id)){
                break;
            }
            System.out.println("查无此编号(输入0取消)");
        }
        return id;
    }

    //录入信息，封装到教师类
    abstract Teacher input_teacher(String id);
}
